package com.sfang.common.extension;

import org.springframework.boot.context.event.*;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.GenericApplicationListener;
import org.springframework.core.ResolvableType;

public class CustomApplicationListener implements GenericApplicationListener {
    @Override
    public boolean supportsEventType(ResolvableType resolvableType) {
        return true;
    }

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationStartingEvent) {
            System.out.println("CustomApplicationListener ApplicationStartingEvent");
        } else if (event instanceof ApplicationEnvironmentPreparedEvent) {
            System.out.println("CustomApplicationListener ApplicationEnvironmentPreparedEvent");

        } else if (event instanceof ApplicationPreparedEvent) {
            System.out.println("CustomApplicationListener ApplicationPreparedEvent");

        } else if (event instanceof ContextClosedEvent && ((ContextClosedEvent) event).getApplicationContext().getParent() == null) {
            System.out.println("CustomApplicationListener ContextClosedEvent");

        } else if (event instanceof ApplicationFailedEvent) {
            System.out.println("CustomApplicationListener ApplicationFailedEvent");

        }else if(event instanceof ApplicationReadyEvent){
            System.out.println("CustomApplicationListener ApplicationReadyEvent");

        }
    }
}
